package com.qinyu.treehole.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qinyu.treehole.annotation.UserAuth;
import com.qinyu.treehole.bo.FeedBo;
import com.qinyu.treehole.entities.Feed;
import com.qinyu.treehole.entities.R;
import com.qinyu.treehole.entities.SimpleFeed;
import com.qinyu.treehole.service.FeedService;
import com.qinyu.treehole.tool.FeedTool;
import com.qinyu.treehole.vo.ForwardFeedVo;
import com.qinyu.treehole.vo.LoginForwardFeedVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/feed")
@Slf4j
@Validated
public class FeedController {
    @Autowired
    private FeedService feedService;

    @UserAuth
    @PostMapping
    public R saveFeed(@Validated FeedBo feedBo){
        log.info("用户id:{}发布动态...", feedBo.getUserId());
        boolean f = feedService.saveFeed(feedBo);
        if(f) return R.ok(null, FeedTool.FEED_SEND_SUCCESS);

        return R.no(null, FeedTool.FEED_SEND_FAIL);
    }

    @UserAuth
    @GetMapping("/follows")
    public R getForwardFeed(@RequestParam("userId")Integer userId,
                            @RequestParam("pageNum") Integer pageNum,
                            @RequestParam("pageSize") Integer pageSize){
        log.info("用户id:{}查询所有关注者的动态+转发", userId);
        PageHelper.startPage(pageNum, pageSize);
        List<SimpleFeed> simpleFeeds = feedService.getForwardAndFeedByUserIdFollow(userId);
        PageInfo info = new PageInfo<>(simpleFeeds);

        List<LoginForwardFeedVo> feeds = feedService.parseFeedToLoginForwardFeedVo(simpleFeeds, userId);
        info.setList(feeds);

        return R.ok(info, FeedTool.FEED_QUERY_SUCCESS);
    }

    @UserAuth
    @PostMapping("/like")
    public R addlikeFeed(@RequestParam("userId")Integer userId,
                            @RequestParam("feedId") Integer feedId){
        log.info("用户id:{}点赞动态id:{}...", userId, feedId);
        Feed feed = feedService.getById(feedId);
        if(feed == null){
            return R.no(null, FeedTool.FEED_NO_EXIST);
        }

        feedService.addLike(userId, feedId);
        return R.ok(null, FeedTool.FEED_LIKE_SUCCESS);
    }

    @UserAuth
    @DeleteMapping("/like")
    public R removelikeFeed(@RequestParam("userId")Integer userId,
                         @RequestParam("feedId") Integer feedId){
        log.info("用户id:{}取消点赞动态id:{}...", userId, feedId);
        Feed feed = feedService.getById(feedId);
        if(feed == null){
            return R.no(null, FeedTool.FEED_NO_EXIST);
        }

        feedService.removeLike(userId, feedId);
        return R.ok(null, FeedTool.FEED_UNLIKE_SUCCESS);
    }

    @GetMapping
    public R getForwardFeed(@RequestParam("pageNum") Integer pageNum,
                        @RequestParam("pageSize") Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<SimpleFeed> simpleFeeds = feedService.getForwardAndFeed();
        PageInfo info = new PageInfo<>(simpleFeeds);

        List<ForwardFeedVo> feeds = feedService.parseFeedToForwardFeedVo(simpleFeeds);
        info.setList(feeds);

        return R.ok(info, FeedTool.FEED_QUERY_SUCCESS);
    }

    @UserAuth
    @GetMapping("/u")
    public R getForwardFeedLogin(@RequestParam("userId")Integer userId,
                                 @RequestParam("pageNum") Integer pageNum,
                                 @RequestParam("pageSize") Integer pageSize){
        log.info("用户id:{}分页查询动态...num:{}, size:{}...", userId, pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<SimpleFeed> simpleFeeds = feedService.getForwardAndFeed();
        PageInfo info = new PageInfo<>(simpleFeeds);

        List<LoginForwardFeedVo> feeds = feedService.parseFeedToLoginForwardFeedVo(simpleFeeds, userId);
        info.setList(feeds);

        return R.ok(info, FeedTool.FEED_QUERY_SUCCESS);
    }
}
